package com.pig4cloud.pig.mt.handler;

import com.pig4cloud.pig.mt.builder.TextBuilder;
import com.pig4cloud.pig.mt.entity.WxInfo;
import com.pig4cloud.pig.mt.service.WxInfoService;
import com.pig4cloud.pig.mt.service.WxMsgReplyService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import me.chanjar.weixin.mp.util.WxMpConfigStorageHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

import static me.chanjar.weixin.common.api.WxConsts.EventType;

@Slf4j
@Component
public class MenuHandler extends AbstractHandler {
    @Autowired
    private WxInfoService sysWxInfoService;

    @Autowired
	WxMsgReplyService msgReplyService;

    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
                                    Map<String, Object> context, WxMpService weixinService,
                                    WxSessionManager sessionManager) {
        String msg = String.format("type:%s, event:%s, key:%s",
                wxMessage.getMsgType(), wxMessage.getEvent(), wxMessage.getEventKey());
        if (EventType.VIEW.equals(wxMessage.getEvent())) {
            return null;
        }

        if (EventType.CLICK.equals(wxMessage.getEvent())) {
            String key = wxMessage.getEventKey();
            if ("TA_START_PUSH".equals(key)) {
                try {
                    WxMpUser userWxInfo = weixinService.getUserService().userInfo(wxMessage.getFromUser(), null);
                    if (userWxInfo != null) {
                        sysWxInfoService.bindMpAppid(userWxInfo);
                        return new TextBuilder().build("成功开始推送", wxMessage, weixinService);
                    }
                } catch (WxErrorException e) {
                    e.printStackTrace();
                }
            } else if ("TA_STOP_PUSH".equals(key)) {
                sysWxInfoService.unbindMpAppid(wxMessage.getFromUser());
                return new TextBuilder().build("成功暂停推送", wxMessage, weixinService);
            } else if ("TA_QUERY_PUSH".equals(key)) {
                String openId = wxMessage.getFromUser();
                if (openId != null) {
                    WxInfo wx = sysWxInfoService.getInfoFromWpAppId(openId);
                    String content;
                    content = wx != null ? "公众号推送中" : "没有打开公众号推送";
//                    if (wx == null) {
//                        content = "Ta在哪儿停止推送\n" +
//                                "<a href=\"weixin://bizmsgmenu?msgmenucontent=启动推送Ta在哪儿&msgmenuid=1\">启动推送Ta在哪儿</a>";
//                    } else {
//                        content = "Ta在哪儿正在推送中\n" +
//                                "<a href=\"weixin://bizmsgmenu?msgmenucontent=取消推送Ta在哪儿&msgmenuid=1\">取消推送Ta在哪儿</a>";
//                    }
                    return new TextBuilder().build(content, wxMessage, weixinService);
                }
            }
            return null;
        }
        String appid = WxMpConfigStorageHolder.get();
        log.info("菜单事件：" + wxMessage.getEventKey());
        msgReplyService.tryAutoReply(appid, true, wxMessage.getFromUser(), wxMessage.getEventKey());

        return WxMpXmlOutMessage.TEXT().content(msg)
                .fromUser(wxMessage.getToUser()).toUser(wxMessage.getFromUser())
                .build();
    }

}
